package com.ndp.fb.walle.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ndp.ec.core.api.AudienceManagerService;
import com.ndp.ec.core.model.EcCustomer;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.model.business.vo.cache.result.TagName;
import com.ndp.fb.model.report.param.EcReportInputParam;
import com.ndp.fb.model.rdb.AudienceManager;
import com.ndp.fb.rdb.model.EntrustAccount;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.redis.api.RedisClient;
import com.ndp.fb.walle.business.AdPublishConditionService;
import com.ndp.fb.walle.business.EcService;
import com.ndp.fb.walle.business.TagForBuildnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by infi.he on 2016/3/18.
 */
@RestController
@RequestMapping("/ec")
public class EcController {

    @Autowired
    private EcService ecService;


    @Reference
    AdPublishConditionService adPublishConditionService;

    @Autowired
    private TagForBuildnameService tagForBuildnameService;

    @Reference
    private AudienceManagerService audienceManagerService;

    @Autowired
    private RedisClient redisClient;


    @RequestMapping("report")
    Map<String, Object> report(@RequestParam(value = "param", required = true) String param, HttpServletRequest req) {
        EcReportInputParam ecReportInputParam = JSON.parseObject(param, EcReportInputParam.class);
        User user = (User) req.getSession().getAttribute("user");
        Map<String, Object> result = ecService.report(user.getOrgId(), user.getTicket(), ecReportInputParam);
        return result;
    }

    @RequestMapping("allWebSite")
    Collection<EcCustomer> allWebSite(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        Collection<EcCustomer> allEcCustomer = ecService.getAllEcCustomerWithFbAccount(user.getTicket());
        return allEcCustomer.isEmpty() ? new ArrayList<>() : allEcCustomer;
    }

    @RequestMapping("allWebSiteByUserId")
    Collection<EcCustomer> allWebSiteByUserId(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        Collection<EcCustomer> allEcCustomer = ecService.getAllEcCustomerByUserId(user.getTicket());
        return allEcCustomer.isEmpty() ? new ArrayList<>() : allEcCustomer;
    }

    @RequestMapping("adActs")
    Collection<EntrustAccount> getAdAccountByWebSite(@RequestParam(required = true) Long prodId, HttpServletRequest req) throws BusinessException {
        User user = (User) req.getSession().getAttribute("user");
        List<EntrustAccount> acts = ecService.findAccountsByProdIdAndUserId(prodId, user.getTicket());
        return acts.isEmpty() ? new ArrayList<>() : acts;
    }

    @RequestMapping("tags")
    Collection<TagName> findTags(HttpServletRequest req) throws BusinessException {
        User user = (User) req.getSession().getAttribute("user");
        List<TagName> tagNames = tagForBuildnameService.buildTagName(user.getLanguage());
        return tagNames.isEmpty() ? new ArrayList<>() : tagNames;
    }

    @RequestMapping("fullCondition")
    Map<String, Object> fullCondition(@RequestParam(required = true) Long feedId) throws BusinessException {
        return ecService.getFullCondition(feedId);
    }

    @RequestMapping("audienceInit")
    Map<String, Object> audienceInit(@RequestParam(value = "weFeedId", required = true) Long weFeedId, @RequestParam(value = "fbAccountId", required = true) Long fbAccountId) throws BusinessException {
        //受众初始化
        //1、受众模板
        List<AudienceManager> audienceTs = audienceManagerService.findAllByFeedAndFbAccountId(weFeedId, fbAccountId);

        //2、自定义受众
        List customAudiences = (List) redisClient.get(CommonConstants.AUDIENCE_REDIS_PREFIX + fbAccountId);

        Map<String, Object> results = new HashMap<>();
        results.put("audience", audienceTs);
        results.put("customAudiences", customAudiences);
        return results;
    }
}
